﻿using ARC.Yunpian.SMSService.Interfaces;
using ARC.Yunpian.SMSService.Models.SimpleSmsModels;
using System.Collections.Generic;
using System.Text;
using System.Text.Json;

namespace ARC.Yunpian.SMSService.Service.SimpleSmsService
{
    public class SimpleSms : BaseYunpian, ISimpleSms
    {
        //Dictionary<string, object> param = new Dictionary<string, object>();
        private readonly string _apiKey;
        public SimpleSms(string apikey)
        {
            _apiKey = apikey;
        }
        public SendSimpleResponse SendSmsOne(string mobile, string text, string extend = "", string uid = "", string callback_url = "", bool register = false,
            bool mobile_stat = true)
        {
            string url = "https://sms.yunpian.com/v2/sms/single_send.json";
            string dataSmsParams = "apikey=" + _apiKey + "&mobile=" + mobile +
                                   "&text=" + text + "&extend=" + extend + "&uid=" + uid + "&callback_url=" + callback_url + "&register=" + register + "&mobile_stat=" + mobile_stat;
            return JsonSerializer.Deserialize<SendSimpleResponse>(Post(url, dataSmsParams));
        }

        public SendSimpleBatchResponse SendSmsBatch(string mobile, string text, string extend = "", string uid = "", string callback_url = "", bool mobile_stat = true)
        {
            string url = "https://sms.yunpian.com/v2/sms/batch_send.json";
            string dataSmsParams = "apikey=" + _apiKey + "&mobile=" + mobile +
                                   "&text=" + text + "&extend=" + extend + "&uid=" + uid + "&callback_url=" + callback_url + "&mobile_stat=" + mobile_stat;
            return JsonSerializer.Deserialize<SendSimpleBatchResponse>(Post(url, dataSmsParams));
        }

        public SendSimpleResponse JustTelempateSendSms(string mobile, string tpl_id, string tpl_value, string callback_url = "", string extend = "",
            string uid = "")
        {
            string url = "https://sms.yunpian.com/v2/sms/tpl_single_send.json";
            string dataSmsParams = "apikey=" + _apiKey + "&mobile=" + mobile +
                                   "&tpl_id=" + tpl_id + "&tpl_value=" + tpl_value + "&callback_url=" + callback_url + "&extend=" + extend + "&uid=" + uid;
            return JsonSerializer.Deserialize<SendSimpleResponse>(Post(url, dataSmsParams));
        }

        public SendSimpleBatchResponse JustTelempateSendSmsBatch(string mobile, string tpl_id, string tpl_value, string callback_url = "", string extend = "",
            string uid = "")
        {
            string url = "https://sms.yunpian.com/v2/sms/tpl_batch_send.json";
            string dataSmsParams = "apikey=" + _apiKey + "&mobile=" + mobile +
                                   "&tpl_id=" + tpl_id + "&tpl_value=" + tpl_value + "&callback_url=" + callback_url + "&extend=" + extend + "&uid=" + uid;
            return JsonSerializer.Deserialize<SendSimpleBatchResponse>(Post(url, dataSmsParams));
        }

        public QuerySimpleSmsSendedResponse QuerySimpleSms(string start_time, string end_time, string mobile = "",
            int page_num = 1, int page_size = 20, string type = "")
        {
            string url = "https://sms.yunpian.com/v2/sms/get_record.json";
            string dataSmsParams = "apikey=" + _apiKey + "&start_time=" + start_time +
                                   "&end_time=" + end_time + "&mobile=" + mobile + "&page_num=" + page_num + "&page_size=" + page_size + "&type=" + type;
            return JsonSerializer.Deserialize<QuerySimpleSmsSendedResponse>(Post(url, dataSmsParams));
        }
        public AddSimpleSmsSinSuccessResponse AddSimpleSinSms(string sign, long prove_type, string license_url, string license_base64, bool notify = true, bool apply_vip = false,
            bool is_only_global = false, string industry_type = "其它")
        {
            string url = "https://sms.yunpian.com/v2/sign/add.json";
            //Dictionary<string, string> aa = new Dictionary<string, string>();
            //aa.Add("sign", sign);
            //aa.Add("prove_type", prove_type.ToString());
            //aa.Add("license_url", license_url);
            //aa.Add("license_base64", license_base64);
            string dataSmsParams = "apikey=" + _apiKey + "&sign=" + sign +
                                   "&prove_type=" + prove_type + "&license_url=" + license_url + "&license_base64=" + System.Web.HttpUtility.UrlEncode(license_base64) + "&notify=" + notify + "&apply_vip=" + apply_vip + "&is_only_global=" + is_only_global + "&industry_type=" + industry_type;
            return JsonSerializer.Deserialize<AddSimpleSmsSinSuccessResponse>(Post(url, dataSmsParams));
        }

        public QuerySimpleSmsSinResponse QuerySimpleSinSms(string sign, int page_num, int page_size)
        {
            string url = "https://sms.yunpian.com/v2/sign/get.json";
            string dataSmsParams = "apikey=" + _apiKey + "&sign=" +System.Web.HttpUtility.UrlEncode(sign) +
                                   "&page_num=" + page_num + "&page_size=" + page_size;
            return JsonSerializer.Deserialize<QuerySimpleSmsSinResponse>(Post(url, dataSmsParams));
        }

        public AddSimpleSmsSinSuccessResponse ModifySimpleSinSms(string old_sign, string sign, long prove_type,
            string license_url, string license_base64, bool notify = true, bool apply_vip = false,
            bool is_only_global = false, string industry_type = "其它")
        {
            string url = "https://sms.yunpian.com/v2/sign/update.json";
            string dataSmsParams = "apikey=" + _apiKey + "&old_sign=" + old_sign + "&sign=" + sign +
                                   "&prove_type=" + prove_type + "&license_url=" + license_url + "&license_base64=" + license_base64 + "&notify=" + notify + "&apply_vip=" + apply_vip + "&is_only_global=" + is_only_global + "&industry_type=" + industry_type;
            return JsonSerializer.Deserialize<AddSimpleSmsSinSuccessResponse>(Post(url, dataSmsParams));
        }
        public AddAndDeleteSimpleSmsTelResponse AddSimpleTelelempleSms(string tpl_content, int notify_type = 0, string website = "", int tplType = 0, string callback = "", string apply_description = "")
        {
            string url = "https://sms.yunpian.com/v2/tpl/add.json";
            string dataSmsParams = "apikey=" + _apiKey + "&tpl_content=" + tpl_content +
                                  "&notify_type=" + notify_type + "&website=" + website + "&tplType=" + tplType + "&callback=" + callback + "&apply_description=" + apply_description;
            return JsonSerializer.Deserialize<AddAndDeleteSimpleSmsTelResponse>(Post(url, dataSmsParams));
        }

        public AddAndDeleteSimpleSmsTelResponse DeleteSimpleTelemplateSms(long tpl_id)
        {
            string url = "https://sms.yunpian.com/v2/tpl/del.json";
            string dataSmsParams = "apikey=" + _apiKey + "&tpl_id=" + tpl_id;
            return JsonSerializer.Deserialize<AddAndDeleteSimpleSmsTelResponse>(Post(url, dataSmsParams));
        }

        public AddAndDeleteSimpleSmsTelResponse ModifySimpleTelelempleSms(long tpl_id, string tpl_content,
            int notify_type = 0, string website = "", int tplType = 0, string callback = "",
            string apply_description = "")
        {
            string url = "https://sms.yunpian.com/v2/tpl/update.json";
            string dataSmsParams = "apikey=" + _apiKey + "&tpl_id=" + tpl_id + "&tpl_content=" + tpl_content +
                                   "&notify_type=" + notify_type + "&website=" + website + "&tplType=" + tplType + "&callback=" + callback + "&apply_description=" + apply_description;
            return JsonSerializer.Deserialize<AddAndDeleteSimpleSmsTelResponse>(Post(url, dataSmsParams));
        }
        public List<QuerySimpleSmsTelTplIdResponse> QuerySimpleTeleplateSms()
        {
            string url = "https://sms.yunpian.com/v2/tpl/get.json";
            string dataSmsParams = "apikey=" + _apiKey;
            return JsonSerializer.Deserialize<List<QuerySimpleSmsTelTplIdResponse>>(Post(url, dataSmsParams));
        }

        public QuerySimpleSmsTelTplIdResponse QuerySimpleTeleplateSms(long tpl_id)
        {
            string url = "https://sms.yunpian.com/v2/tpl/get.json";
            string dataSmsParams = "apikey=" + _apiKey + "&tpl_id=" + tpl_id;
            return JsonSerializer.Deserialize<QuerySimpleSmsTelTplIdResponse>(Post(url, dataSmsParams));
        }
    }
}
